package cn.wolfcode.wolf2w.business.util;

import cn.wolfcode.wolf2w.common.core.exception.ServiceException;
import org.apache.http.HttpResponse;

import java.rmi.ServerException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 浪兎兎
 * @since 2025-05-29-11:49
 */
public abstract class SMSUtil {
    public static HttpResponse sendAliyunMessage(String phone, String code) {
        String host = "https://dfsns.market.alicloudapi.com";
        String path = "/data/send_sms";
        String method = "POST";
        String appcode = "1c80c0e4550e4f47842622e63be58953";
        Map<String, String> headers = new HashMap<>();
        headers.put("Authorization", "APPCODE " + appcode);
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        Map<String, String> querys = new HashMap<>();
        Map<String, String> bodys = new HashMap<>();
        bodys.put("content", "code:" + code);
        bodys.put("template_id", "CST_ptdie100");
        bodys.put("phone_number", phone);
        try {
            HttpResponse httpResponse = HttpUtils.doPost(host, path, method, headers, querys, bodys);
            int statusCode = httpResponse.getStatusLine().getStatusCode();
            if (statusCode != 200) {
                throw new ServiceException(String.format("发送验证码失败 (code: %d)", statusCode));
            }
            return httpResponse;
        } catch (Exception e) {
            throw new ServiceException(e.getMessage());
        }
    }
}
